<template>
  <!-- TODO: add by axiang [20190702] 交互很难看，需要重新设计 -->
  <div class="docker">
    <el-card class="box-card">
      <div slot="header">讨论区</div>
      <div class="functionBar">
        <el-pagination
          style="float:left"
          layout="prev, pager, next"
          :current-page="currentPage"
          @current-change="getList"
          :total="this.currentTotal"
        ></el-pagination>
        <el-button
          v-if="isLogin"
          style="float:right;margin-right:10px;margin-top:10px"
          @click="newDiscuss"
          size="mini"
        >新建帖子</el-button>
         <el-button
                size="mini"
                class="bar-search-item"
                type="primary"
                icon="el-icon-search"
                style="float:right;margin-right:10px;margin-top:10px"
          @click="getSearch(1)"
        >搜索</el-button>
        <el-input
          v-model="input"
          placeholder="输入名字或者标题"
          size="mini"
          class="input"
        >
          <!-- <template slot="prepend">搜索</template> -->
        </el-input>
      </div>
      <el-table
        style="width:100%;"
        :data="this.tableData"
        highlight-current-row
        v-loading="loading"
      >
        <el-table-column
          prop="id"
          label="#"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          width=""
        >
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="toChat(scope.row)"
            >{{scope.row.title}}</el-link>
          </template>
        </el-table-column>
        <el-table-column
          prop="time"
          label="发布时间"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="author"
          label="发布人"
          width="200"
        >
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="toUser(scope.row)"
            >{{scope.row.author}}</el-link>
          </template>
        </el-table-column>
        <!-- <el-table-column
          label="最后回复时间"
          width="200"
        ></el-table-column>
        <el-table-column
          label="最后回复人"
          width="200"
        ></el-table-column> -->
      </el-table>
    </el-card>
  </div>
</template>
<script>
export default {
  data () {
    return {
      input: '',
      loading: '',
      currentPage: 1,
      currentTotal: 0,
      tableData: [],
      isSearch: false,
      isLogin: false
    }
  },
  methods: {
    async getDiscuss (val) {
      this.tableData = []
      this.loading = true
      let params = new URLSearchParams()
      params.append('pageNum', val)
      params.append('pageSize', 10)
      this.currentPage = val
      let dataDiscuss = await this.$http
        .get('/discuss/post/list', params)
        .catch(() => { })
      this.loading = false
      this.currentTotal = dataDiscuss.datas[1]
      this.tableData = dataDiscuss.datas[0]
    },
    async getList (val) {
      if (this.isSearch) {
        this.getSearch(val)
      } else {
        this.getDiscuss(val)
      }
    },
    async getSearch (val) {
      this.tableData = []
      this.isSearch = true
      this.loading = true
      let params = new URLSearchParams()
      params.append('pageNum', val)
      params.append('pageSize', 10)
      params.append('content', this.input)
      this.currentPage = val
      let dataDiscussByTitleOrNickName = await this.$http
        .get('/discuss/getDiscussByTitleOrNickName', params)
        .catch(() => {
          this.$message({
            message: '服务器繁忙，请稍后再试！',
            type: 'error'
          })
        })
      this.loading = false
      this.currentTotal = dataDiscussByTitleOrNickName.datas[1]
      this.tableData = dataDiscussByTitleOrNickName.datas[0]
    },
    toChat (row) {
      this.$router.push({
        path: 'Chat',
        query: { title: row.title, id: row.discussid }
      })
    },
    toUser (row) {
      this.$router.push({ path: '/User', query: { username: row.author } })
    },
    newDiscuss () {
      this.$prompt('请输入标题', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(({ value }) => {
          this.insertDiscuss(value)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消创建'
          })
        })
    },
    async insertDiscuss (val) {
      let params = new URLSearchParams()
      params.append('title', val)
      params.append('author', this.$store.getters.getUsername)
      let dataInsertDiscuss = await this.$http.post('/discuss/putDiscuss', params)
      if (dataInsertDiscuss.code === 10000) {
        this.$message.success('新建帖子成功!')
        this.getDiscuss(this.currentPage)
      } else {
        this.$message.error('服务器繁忙，请稍后再试!')
      }
      this.$message({ message: dataInsertDiscuss.msg, type: 'success' })
    },
    getLoginStatu () {
      if (this.$store.getters.getUsername) {
        this.isLogin = true
      } else {
        this.isLogin = false
      }
    }
  },
  mounted () {
    if (this.$store.getters.getIsLogin) {
      this.getLoginStatu()
      this.getDiscuss(this.currentPage)
    } else {
      this.$notify({
        title: '提示',
        message: '登录后才能查看讨论区哦',
        type: 'warning',
        offset: 100,
        duration: 3000
      })
    }
  }
}
</script>
<style scoped>
.docker {
  width: 100%;
  min-height: 700px;
  margin: 0;
  padding: 0;
}

.box-card {
  min-height: 600px;
  width: 75%;
  margin: auto;
  padding: 0;
}

.discuss-head {
  background-color: #f5f5f5;
  height: 20px;
  width: 100%;
  margin: 0;
  border-bottom: 1px solid #eeeeee;
  padding: 10px 15px;
  display: block;
}
.pagination {
  float: left;
  margin-left: 10px;
  height: 30px;
  margin-top: 10px;
}

.functionBar {
  width: 100%;
  margin: 0;
  padding: 0;
  height: 50px;
  border-bottom: 1px solid silver;
}

.input {
  float: right;
  margin-right: 10px;
  margin-top: 10px;
  height: 30px;
  width: 200px;
}

.button {
  float: right;
  margin-right: 10px;
  margin-top: 10px;
  height: 30px;
  width: 50px;
}
</style>
